<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\exception\UnSupportedOperationException;
use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;

/**
 * manager_menu action
 * @package admin\action
 * @author yangjian<yangjian102621@gmail.com>
 */
class ManagerMenuAction extends CommonAction {

    protected $serviceBean = 'admin.menu.service';

    protected $actionTitle = '菜单管理';

    //数据列表页面
    public function index( HttpRequest $request ) {

        $name = $request->getParameter('name', 'trim');

        $conditions = array();
        if ( $name ) $conditions['name'] = "%{$name}%";
        if ( !$conditions ) $conditions = array('pid' => 0);

        $service = Beans::get($this->getServiceBean());
        $items = $service->where($conditions)
            ->sort(array('sort_num' => 1))
            ->find();
        foreach ( $items as $key => $value ) {
            $items[$key]['subs'] = $service->where(array('pid' => $value['id']))
                ->sort(array('sort_num' => 1))
                ->find();
        }

        $this->loadParentMenus();

        $this->assign('items', $items);
        $this->setView('system/menu_index');
        $this->assign("title", $this->actionTitle."列表");
        $this->assign('breadcrumb', array('系统管理',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));
    }

    //数据添加页面
    public function add(HttpRequest $request) {
        throw new UnSupportedOperationException('暂时不支持该操作.');
    }

    //数据编辑页面
    public function edit(HttpRequest $request) {
        $id = $request->getParameter('id', 'intval');
        if ( $id <= 0 ) {
            AjaxResult::ajaxFailtureResult();
        }
        $service = Beans::get($this->getServiceBean());
        $item = $service->getItem($id);
        if ( empty($item) ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, self::NO_RECORDS);
        }
        AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS, null, $item);
    }

    //加载父类菜单
    protected function loadParentMenus() {
        $service = Beans::get($this->getServiceBean());
        $menus = $service->where(array('pid' => 0))
            ->sort(array('sort_num' => 1))
            ->find();
        $this->assign('menus', $menus);
    }

    //显示|隐藏菜单
    public function set(HttpRequest $request) {

        $service = Beans::get($this->getServiceBean());
        parent::set($request, function() use ($service) {
            $service->updateMenuCache(); //更新缓存
        });
    }

    //插入数据操作
    public function insert(HttpRequest $request) {

        $data = $request->getParameter('data');
        $service = Beans::get($this->getServiceBean());
        $data['sort_num'] = $service->count();
        parent::__insert($data, function($service) {
            $service->updateMenuCache(); //更新缓存
        });
    }

    //更新数据操作
    public function update(HttpRequest $request) {

        $data = $request->getParameter('data');
        $id = $request->getParameter('id', 'trim');
        parent::__update($data, $id, function($service) {
            $service->updateMenuCache(); //更新缓存
        });

    }

    //交换排序操作
    public function exchangeSort(HttpRequest $request) {
        $orgId = $request->getParameter('org_id', 'intval');
        $distId = $request->getParameter('dist_id', 'intval');
        $orgSort = $request->getParameter('org_sort', 'intval');
        $distSort = $request->getParameter('dist_sort', 'intval');
        $service = Beans::get($this->getServiceBean());
        $service->set('sort_num', $orgSort, $orgId);
        $service->set('sort_num', $distSort, $distId);
        $service->updateMenuCache(); //更新缓存
        AjaxResult::ajaxSuccessResult();
    }

    //删除菜单
    public function delete( HttpRequest $request ) {

        $id = $request->getParameter('id', 'trim');
        if ( empty($id) ) AjaxResult::ajaxResult('error', self::NO_RECORDS);
        $service = Beans::get($this->getServiceBean());

        //判断是否还有子菜单
        if ( $service->count(array('pid' => $id)) > 0 ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, '请先删除子菜单');
        }

        if ( $service->delete($id) ) {
            $service->updateMenuCache(); //更新缓存
            AjaxResult::ajaxSuccessResult();
        } else {
            AjaxResult::ajaxFailtureResult();
        }
    }
}
